package com.yvtian.inv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yvtian.inv.pojo.*;
import com.yvtian.inv.dao.InvMaterialReturnMapper;
import com.yvtian.inv.service.InvMaterialReturnItemService;
import com.yvtian.inv.service.InvMaterialReturnService;
import com.yvtian.inv.service.ShopService;
import com.yvtian.utils.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 生产退料单(inv_material_return)表服务实现类
 *
 * @author long
 * @since 2024-07-02 13:43:38
 */
@Service("invMaterialReturnService")
public class InvMaterialReturnServiceImpl extends ServiceImpl<InvMaterialReturnMapper, InvMaterialReturnEntity> implements InvMaterialReturnService {

    @Autowired
    private InvMaterialReturnItemService invMaterialReturnItemService;
    @Autowired
    private ShopService shopService;

    @Override
    public IPage<InvMaterialReturnEntity> findByPage(Page<InvMaterialReturnEntity> page, InvMaterialReturnEntity invMaterialReturnEntity) {
        LambdaQueryWrapper<InvMaterialReturnEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(InvMaterialReturnEntity::getIsDel, 0);
        getQueryWrapper(invMaterialReturnEntity, queryWrapper);
        return this.page(page, queryWrapper);
    }

    @Transactional
    @Override
    public boolean insertOrUpdateAll(InvMaterialReturnEntity invMaterialReturnEntity) {
        ArrayList<InvMaterialReturnItemEntity> itemList = invMaterialReturnEntity.getInvMaterialReturnItemList();
        if (itemList != null && !itemList.isEmpty()) {
            LambdaUpdateWrapper<InvMaterialReturnItemEntity> itemUpdateWrapper = new LambdaUpdateWrapper<>();
            itemUpdateWrapper.eq(InvMaterialReturnItemEntity::getZcode,invMaterialReturnEntity.getCode());
            invMaterialReturnItemService.remove(itemUpdateWrapper);
            invMaterialReturnItemService.saveBatch(itemList);
        }
        this.saveOrUpdate(invMaterialReturnEntity);
        return true;
    }

    @Transactional
    @Override
    public boolean handleStorage(InvMaterialReturnEntity invMatReturnEntity) {
        //查询子表领取的数量
        List<InvMaterialReturnItemEntity> itemEntityList = invMaterialReturnItemService.list(new LambdaQueryWrapper<InvMaterialReturnItemEntity>()
                .eq(InvMaterialReturnItemEntity::getZcode, invMatReturnEntity.getCode()));
        for (InvMaterialReturnItemEntity matReturn : itemEntityList) {
            if (matReturn.getReturnCount() == null || matReturn.getReturnCount().compareTo(BigDecimal.ZERO) <= 0) {
                continue;
            }
            //更新商品库存
            LambdaUpdateWrapper<ShopEntity> shopEntityWrapper = new LambdaUpdateWrapper<>();
            shopEntityWrapper.setSql("quantity = quantity + " + matReturn.getReturnCount());
            shopEntityWrapper.eq(ShopEntity::getShopCode, matReturn.getShopCode());
            shopService.update(shopEntityWrapper);
        }

        //更新主表状态，（审核）
        LambdaUpdateWrapper<InvMaterialReturnEntity> invMarReturnUpdateWrapper = new LambdaUpdateWrapper<>();
        invMarReturnUpdateWrapper.eq(InvMaterialReturnEntity::getCode, invMatReturnEntity.getCode());
        invMarReturnUpdateWrapper.set(InvMaterialReturnEntity::getStatus, 1);
        invMarReturnUpdateWrapper.set(InvMaterialReturnEntity::getCheckName, invMatReturnEntity.getCheckName());
        invMarReturnUpdateWrapper.set(InvMaterialReturnEntity::getCheckUser, invMatReturnEntity.getCheckUser());
        invMarReturnUpdateWrapper.set(InvMaterialReturnEntity::getCheckTime, new Date());
        this.update(invMarReturnUpdateWrapper);
        return true;
    }

    private void getQueryWrapper(InvMaterialReturnEntity invMaterialReturnEntity, LambdaQueryWrapper<InvMaterialReturnEntity> queryWrapper) {
        if (StringHelper.isNotEmpty(invMaterialReturnEntity.getReturnTimeStr())) {
            String[] split = invMaterialReturnEntity.getReturnTimeStr().split("~");
            queryWrapper.between(InvMaterialReturnEntity::getReturnTimeStr, split[0], split[1]);
        }
        queryWrapper
                .like(StringHelper.isNotEmpty(invMaterialReturnEntity.getCode()), InvMaterialReturnEntity::getCode, invMaterialReturnEntity.getCode())
                .like(StringHelper.isNotEmpty(invMaterialReturnEntity.getInvOrderCode()), InvMaterialReturnEntity::getInvOrderCode, invMaterialReturnEntity.getInvOrderCode())
                .like(StringHelper.isNotEmpty(invMaterialReturnEntity.getStockCode()), InvMaterialReturnEntity::getStockCode, invMaterialReturnEntity.getStockCode())
                .like(StringHelper.isNotEmpty(invMaterialReturnEntity.getStockName()), InvMaterialReturnEntity::getStockName, invMaterialReturnEntity.getStockName())
                .like(StringHelper.isNotEmpty(invMaterialReturnEntity.getReturnUsername()), InvMaterialReturnEntity::getReturnUsername, invMaterialReturnEntity.getReturnUsername())
                .like(StringHelper.isNotEmpty(invMaterialReturnEntity.getReturnAccount()), InvMaterialReturnEntity::getReturnAccount, invMaterialReturnEntity.getReturnAccount())
                .like(StringHelper.isNotEmpty(invMaterialReturnEntity.getProjectCode()), InvMaterialReturnEntity::getProjectCode, invMaterialReturnEntity.getProjectCode())
                .like(StringHelper.isNotEmpty(invMaterialReturnEntity.getProjectName()), InvMaterialReturnEntity::getProjectName, invMaterialReturnEntity.getProjectName())
                .eq(invMaterialReturnEntity.getStatus() != null, InvMaterialReturnEntity::getStatus, invMaterialReturnEntity.getStatus())
                .like(StringHelper.isNotEmpty(invMaterialReturnEntity.getDelName()), InvMaterialReturnEntity::getDelName, invMaterialReturnEntity.getDelName())
                .like(StringHelper.isNotEmpty(invMaterialReturnEntity.getCreateName()), InvMaterialReturnEntity::getCreateName, invMaterialReturnEntity.getCreateName())
                .like(StringHelper.isNotEmpty(invMaterialReturnEntity.getCreateUser()), InvMaterialReturnEntity::getCreateUser, invMaterialReturnEntity.getCreateUser())
                .like(StringHelper.isNotEmpty(invMaterialReturnEntity.getUpdateName()), InvMaterialReturnEntity::getUpdateName, invMaterialReturnEntity.getUpdateName())
                .like(StringHelper.isNotEmpty(invMaterialReturnEntity.getUpdateUser()), InvMaterialReturnEntity::getUpdateUser, invMaterialReturnEntity.getUpdateUser())
                .like(StringHelper.isNotEmpty(invMaterialReturnEntity.getRemarks()), InvMaterialReturnEntity::getRemarks, invMaterialReturnEntity.getRemarks())
                .like(StringHelper.isNotEmpty(invMaterialReturnEntity.getCheckName()), InvMaterialReturnEntity::getCheckName, invMaterialReturnEntity.getCheckName())
                .like(StringHelper.isNotEmpty(invMaterialReturnEntity.getCheckUser()), InvMaterialReturnEntity::getCheckUser, invMaterialReturnEntity.getCheckUser());
    }
}

